<?php
namespace app\api\controller;

use app\api\controller\Authbase;
use app\api\model\AlbumModel;
use app\api\model\SystemMusicModel;
use app\common\model\project_model\SystemMusicCategoryModel;
use think\Controller;

/**
 * 相册音乐接口
 */
class AlbumMusic extends Authbase
{
    /**
     * 相册音乐类型列表及所有音乐简化数据
     * 每页5个
     */
    public function cate_index() {
        $keyword = input('keyword', null);
        $music = SystemMusicModel::musicCategory($keyword);
        return json(['code' => 1, 'msg' => 'success', 'data' => $music]);
    }

    /**
     * 分类下的所有音乐
     *
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index() {
        $cate_id = input('id/d', '');
        if (empty($cate_id)) {
            return json(['code' => 0, 'error' => '请传递参数id']);
        }
        $music = SystemMusicModel::musicByCateId($cate_id);
        return json(['code' => 1, 'msg' => 'success', 'data' => $music]);
    }

    /**
     * 选取音乐
     *
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function select() {
        $id = input('id', ''); // 音乐id
        $album_id = input('album_id', ''); // 相册id
        if (empty($id) || empty($album_id)) {
            return json(['code' => 0, 'error' => '请传递参数id和album_id']);
        }
        $AlbumMusic = SystemMusicModel::musicById($id);

        // 更新相册
        if (!$album = AlbumModel::up($album_id, ['music_id' => $AlbumMusic['music_id'], 'music_name' => $AlbumMusic['title'], 'music_url' => $AlbumMusic['mp3_url']])) {
            return json(['code' => 0, 'error' => AlbumModel::$error_msg]);
        }

        // 获取相册
        return json(['code' => 1, 'msg' => 'success']);
    }

    /**
     * 音乐播放请求url
     *
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function play() {
        $mid = input('music_id');
        if (!$url = SystemMusicModel::requestPlay($mid)) {
            return json(['code' => 0, 'error' => SystemMusicModel::$err_msg]);
        }
        return json(['code' => 1, 'msg' => 'success', 'mp3_url' => $url]);
    }


}